package ar.edu.taxi.persistencia;

import java.io.File;
import java.sql.*;
import ar.edu.taxi.util.DBConfig;

public class DataAccesObject {
	private String host;
	private String dataBase;
	private String user;
	private String pass;
	
	public DataAccesObject() {
		DBConfig dbConfig;
		String file = ".\\config\\config.cfg";
		File f = new File(file);
		if( f.exists() ){
			dbConfig = new DBConfig(file);
			this.host = dbConfig.getHost();
			this.dataBase = dbConfig.getDataBase();
			this.user = dbConfig.getUser();
			this.pass = dbConfig.getPass();	
		}else{
			this.host = "localhost";
			this.dataBase = "taxis";
			this.user = "root";
			this.pass = "root";
		}
	}
	
	public Connection getConexion(){
		Connection conexion = null; 
		try{
			Class.forName("org.gjt.mm.mysql.Driver");
			conexion = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dataBase, user, pass);
		}catch(Exception e){
			e.printStackTrace();
		}
		return conexion;
	 }
	
	public Connection getConexion(String db, String usr, String pass){
		Connection conexion = null; 
		try{
			Class.forName("org.gjt.mm.mysql.Driver");
			conexion = DriverManager.getConnection(db, usr, pass);
		}catch(Exception e){
			e.printStackTrace();
		}
		return conexion;
	 }
	
	public void cerrarConexion(Connection conexion){
		try {
			conexion.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
